package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Jigouzhongxinduanaccount;
import com.yl.auth.entity.Role;
import com.yl.auth.entity.Users;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-20
 */
@RestController
@RequestMapping("/admin/yanglao/jigouzhongxinduanaccount")
@Api(tags = "机构中心端-账号管理")
public class JigouzhongxinduanaccountController {
    @Resource
    private JigouzhongxinduanaccountService jigouzhongxinduanaccountService;

    @Resource
    private JigouzhongxinduanroleService jigouzhongxinduanroleService;

    @Resource
    private RoleService roleService;
    @Resource
    private UsersService usersService;
    @ApiOperation("全查+分页查询账号信息")
    @GetMapping("/findAllAndPage/{page}/{limit}")
    public Result findAllAndPage(@PathVariable Integer page,@PathVariable Integer limit){
        Page<Jigouzhongxinduanaccount> page1 = new Page<>(page,limit);
        Page<Jigouzhongxinduanaccount> jigouzhongxinduanaccountPage = jigouzhongxinduanaccountService.findAllAndPage(page1);
        return Result.ok(jigouzhongxinduanaccountPage);
    }

    @ApiOperation("查询全部角色信息")
    @GetMapping("/findAllRole")
    public Result findAllRole(){
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getType,2);
        List<Role> list = roleService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }

    @ApiOperation("新增账号信息以及角色信息")
    @PostMapping("/saveAccount/{idList}")
    public Result saveAccount(@ApiParam("机构中心端账号实体类")@RequestBody Jigouzhongxinduanaccount jigouzhongxinduanaccount,@PathVariable List<Long> idList){
        jigouzhongxinduanaccount.setCreateTime(new Date());
        Users users = jigouzhongxinduanaccount.getUsers();
        String name = jigouzhongxinduanaccount.getName();
        String phone = jigouzhongxinduanaccount.getPhone();
        String headSculpture = jigouzhongxinduanaccount.getHeadSculpture();
        users.setName(name);
        users.setPhone(phone);
        users.setImgurl(headSculpture);
        users.setType("2");
        usersService.save(users);
        Integer id = usersService.getMax();
        jigouzhongxinduanaccount.setUsersId(id);
        jigouzhongxinduanaccountService.save(jigouzhongxinduanaccount);
        jigouzhongxinduanaccountService.saveAccount(jigouzhongxinduanaccount.getId(),idList);
        return Result.ok();
    }

    //private static Jigouzhongxinduanaccount jigouzhongxinduanaccount = new Jigouzhongxinduanaccount();
//
//    @GetMapping("/zlogin/{username}/{password}")
//    @ApiOperation(value = "中心端登录功能")
//    public Result zlogin(@PathVariable @ApiParam("用户名") String username ,@PathVariable @ApiParam("密码") String password) {
//        LambdaQueryWrapper<Jigouzhongxinduanaccount> jigouzhongxinduanaccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        jigouzhongxinduanaccountLambdaQueryWrapper.eq(Jigouzhongxinduanaccount::getUsername,username);
//        jigouzhongxinduanaccountLambdaQueryWrapper.eq(Jigouzhongxinduanaccount::getPassword,password);
//        jigouzhongxinduanaccount = jigouzhongxinduanaccountService.getOne(jigouzhongxinduanaccountLambdaQueryWrapper);
//        if(jigouzhongxinduanaccount!=null) {
//            return Result.ok();
//        }else{
//            return Result.fail();
//        }
//    }

//    @GetMapping("/getJiGouZhongXin/{id}")
//    @ApiOperation(value = "回显机构中心账号设置信息")
//    public Result getJiGouZhongXin(@PathVariable Integer id) {
////        Long id = jigouzhongxinduanaccount.getId();
////        String roleName = jigouzhongxinduanaccountService.getRoleName(id);
////        jigouzhongxinduanaccount.setRoleName(roleName);
////        System.out.println(jigouzhongxinduanaccount.getRoleName());
//        Jigouzhongxinduanaccount jigouzhongxinduanaccount =  jigouzhongxinduanaccountService.getJiGouZhongXin(id);
//        return Result.ok(jigouzhongxinduanaccount);
//    }

//    @GetMapping("/getRoleId")
//    @ApiOperation(value = "回显角色姓名")
//    public Result getRoleId() {
//        Jigouzhongxinduanrole jigouzhongxinduanrole = jigouzhongxinduanroleService.getById(jigouzhongxinduanaccount.getRoleName());
//        return Result.ok(jigouzhongxinduanrole);
//    }

    @ApiOperation("根据账号id查看该账号信息以及角色")
    @GetMapping("/getByAccountId/{id}")
    public Result getByAccountId(@PathVariable Long id){
        Jigouzhongxinduanaccount jigouzhongxinduanaccount = jigouzhongxinduanaccountService.getById(id);
        Integer usersId = jigouzhongxinduanaccount.getUsersId();
        Users users = usersService.getById(usersId);
        List<Long> idList = jigouzhongxinduanaccountService.getRoleByAccount(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("account",jigouzhongxinduanaccount);
        map.put("idList",idList);
        map.put("users",users);
        return Result.ok(map);
    }
    @ApiOperation("修改账号信息以及角色信息")
    @PostMapping("/updateAccount")
    public Result updateAccount(@RequestParam(value = "idList") List<Long> idList,@RequestBody Jigouzhongxinduanaccount jigouzhongxinduanaccount){
        //先清空角色信息
        Long id = jigouzhongxinduanaccount.getId();
        jigouzhongxinduanaccountService.deleteByRoleId(id);
        usersService.updateById(jigouzhongxinduanaccount.getUsers());
        //再修改账号以及角色信息
        jigouzhongxinduanaccountService.updateById(jigouzhongxinduanaccount);
        jigouzhongxinduanaccountService.saveAccount(jigouzhongxinduanaccount.getId(),idList);
        return Result.ok();
    }

    @ApiOperation("根据id删除账号信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        jigouzhongxinduanaccountService.removeById(id);
        jigouzhongxinduanaccountService.deleteByRoleId(id);
        return Result.ok();
    }
}

